package com.liuf.di.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuf.di.api.ServiceException;
import com.liuf.di.configuration.kettle.KettleInitFactory;
import com.liuf.di.repository.IJobRepository;
import com.liuf.di.service.IJobService;
import org.pentaho.di.core.Result;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.repository.LongObjectId;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.pentaho.di.repository.Repository;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @program: spring-cloud-liuf-master
 * @description: 任务管理业务类
 * @author: Mr.Liu
 * @create: 2018-09-20 09:05
 * @码云： https://gitee.com/www.jiandan.com
 **/
@Service
public class JobServiceImpl implements IJobService {

    Logger logger = LoggerFactory.getLogger(JobServiceImpl.class);

    /**资源库*/
    private Repository repository = null;

    private ObjectId objectId = null;

    @Autowired
    private KettleInitFactory kettleInitFactory;

    private JobMeta jobMeta = null;

    /**任务执行结果*/
    private Result result = null;


    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private IJobRepository jobRepository;

    @Override
    public PageInfo findJob(Map<String,Object> params) throws ServiceException {
        PageHelper.startPage((Integer) params.get("pageNum"),(Integer) params.get("pageSize"));
        return new PageInfo(jobRepository.findJob(params));
    }

    @Override
    public Map<String, Object> getJob(long id) throws ServiceException {
        return jobRepository.getJob(id);
    }

    @Override
    public JobMeta loadJob(String jobname, long id,Repository repository) throws ServiceException {
        jobMeta = new JobMeta();
        try {
            RepositoryDirectoryInterface dir = repository.findDirectory(new LongObjectId(id));
            jobMeta = repository.loadJob(jobname,dir,null,null);
        } catch (KettleException e) {
            logger.error("加载JOB【任务】失败,请联系管理员！");
            e.printStackTrace();
        }
        return jobMeta;
    }

    @Override
    public void delJob(long id) throws ServiceException {
        /**初始化资源库*/
        repository = kettleInitFactory.getRepositoryInit();
        try {
            ((KettleDatabaseRepository) repository).delJob(new LongObjectId(id));
        } catch (KettleException e) {
            logger.error("删除JOB[任务]失败,请联系管理员！");
            e.printStackTrace();
        }finally {
            if(repository != null) {
                KettleInitFactory.destroy(repository);
                repository = null;
            }
        }
    }

    @Override
    public Result jobExecution(String jobname, long id) throws ServiceException{
        /**初始化资源库*/
        repository = kettleInitFactory.getRepositoryInit();
        JobMeta jobMeta = this.loadJob(jobname,id,repository);
        Job job = new Job(repository,jobMeta);
        try {
//            job.start();
            result = job.execute(0,null);
            job.waitUntilFinished();
            if (job.getErrors() > 0){
                System.out.println("decompress fail!");
             }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("JOB任务执行出现错误,请联系系统搞管理员！");
            return null;
        }finally {
            KettleInitFactory.destroy(repository);
            repository = null;
        }
    }

}
